package com.example.demo.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.service.UserService;

@RestController
@RequestMapping("/api")
public class IndexController {

    @Autowired
    private UserService userService;


    @GetMapping("/getById")
    public Map<String, Object> getById(Long id) throws Exception{

        long start = System.currentTimeMillis();

        Map<String, Object> map = new HashMap<>();
        List<Future<String>> futures = new ArrayList<>();
        for (long i = 0; i < 100; i++) {
            Future<String> future = userService.getById(i);
            futures.add(future);
        }
        List<String> response = new ArrayList<>();
        for (Future future : futures) {
            String string = (String) future.get();
            response.add(string);
        }
        map.put("data", response);
        map.put("消耗时间", String.format("任务执行成功,耗时{%s}毫秒", System.currentTimeMillis() - start));
        System.out.println(String.format("任务执行成功,耗时{%s}毫秒", System.currentTimeMillis() - start));
        return map;
    }


    @GetMapping("/getById2")
    public Map<String, Object> getById2(Long id) throws Exception{
        long start = System.currentTimeMillis();

        Map<String, Object> map = new HashMap<>();
        List<String> response = userService.getById2(id);
        map.put("data", response);
        map.put("消耗时间", String.format("任务执行成功,耗时{%s}毫秒", System.currentTimeMillis() - start));

        System.out.println(String.format("任务执行成功,耗时{%s}毫秒", System.currentTimeMillis() - start));
        return map;
    }

    @GetMapping("/getById3")
    public Map<String, Object> getById3(Long id) throws Exception{

        long start = System.currentTimeMillis();
        Map<String, Object> map = new HashMap<>();
        List<String> response = new ArrayList<>();
        for (long i = 0; i < 10; i++) {
            String future = userService.getById3(i);
            response.add(future);
        }
        map.put("data", response);
        map.put("消耗时间", String.format("任务执行成功,耗时{%s}毫秒", System.currentTimeMillis() - start));
        System.out.println(String.format("任务执行成功,耗时{%s}毫秒", System.currentTimeMillis() - start));
        return map;
    }

}


































